/*************************************************
filename	: mesh.h
author		: Stefan lulham
desc		: A mesh is a collection of vertices
			  and triangles.
*************************************************/
#ifndef D_MESH_H
#define D_MESH_H

#include <iostream>
#include <vector>
#include "indexed_triangle.h"
#include "vertex.h"
#include <helpers\math\delge_math.h>

using namespace std;
using namespace DelGE::Helpers::Math;

namespace DelGE
{
	namespace Modules
	{
		namespace Graphics
		{
			class Mesh
			{
			private:
				vector<Vertex*> vVertexList;
				vector<IndexedTriangle*> vIndexedTriangleList;

				float *pfVertexArray;
				float *pfTextureCoordArray;
				float *pfColorArray;
				float *pfNormalArray;
				unsigned int  *piIndexArray;

				bool bNeedsRebuilding;

			public:
				Mesh();
				Mesh(Mesh *pMesh);
				~Mesh();

				static Mesh* CreateCube(float fWidth, float fHeight, float fDepth, float fXOffset = -0.5f, float fYOffset = -0.5f, float fZOffset = -0.5f);
				static Mesh* CreatePlane(float fWidth, float fDepth, float fXOffset = -0.5f, float fZOffset = -0.5f);

				void AddVertex(float fX, float fY, float fZ, float fU = 0, float fV = 0, const Color& rColor = DELGE_GRAPHICSCOLOR_WHITE, float fNormalX = 0, float fNormalY = 0, float fNormalZ = 0);
				void AddIndexedTriangle(int iIndex0, int iIndex1, int iIndex2);

				void RemoveVertex(int iIndex);
				void RemoveIndexedTriangle(int iIndex);

				void ClearMesh();
				void BuildMesh();
				void UpdateVertexPositions();

				Vertex* GetVertex(unsigned int iIndex);
				IndexedTriangle* GetIndexedTriangle(unsigned int iIndex);

				float* GetVertexArray();
				float* GetTextureCoordArray();
				float* GetColorArray();
				float* GetNormalArray();
				unsigned int* GetIndexArray();

				vector<Vertex*>& GetVertices();
				vector<IndexedTriangle*>& GetIndexedTriangles();

				int GetVertexCount();
				int GetIndexedTriangleCount();

				bool NeedsRebuilding();

				// Transform mesh verticies
				void Rotate(float fPitch, float fYaw, float fRoll);
				void Translate(float fX, float fY, float fZ);
				void Scale(float fScaleX, float fScaleY, float fScaleZ);
			};
		}
	}
}

#endif